package com.dianping.cat.consumer.transaction.model.transform;

import com.dianping.cat.consumer.transaction.model.IEntity;
import com.dianping.cat.consumer.transaction.model.entity.AllDuration;
import com.dianping.cat.consumer.transaction.model.entity.Duration;
import com.dianping.cat.consumer.transaction.model.entity.Graph;
import com.dianping.cat.consumer.transaction.model.entity.Graph2;
import com.dianping.cat.consumer.transaction.model.entity.GraphTrend;
import com.dianping.cat.consumer.transaction.model.entity.Machine;
import com.dianping.cat.consumer.transaction.model.entity.Range;
import com.dianping.cat.consumer.transaction.model.entity.Range2;
import com.dianping.cat.consumer.transaction.model.entity.StatusCode;
import com.dianping.cat.consumer.transaction.model.entity.TransactionName;
import com.dianping.cat.consumer.transaction.model.entity.TransactionReport;
import com.dianping.cat.consumer.transaction.model.entity.TransactionType;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



















public class DefaultSaxParser
  extends DefaultHandler
{
/*  55 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  57 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  59 */   private Stack<String> m_tags = new Stack();

/*  61 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  65 */   private StringBuilder m_text = new StringBuilder();


/*  68 */   public static TransactionReport parse(InputStream in) throws SAXException, IOException { return (TransactionReport)parseEntity(TransactionReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  72 */   public static TransactionReport parse(Reader reader) throws SAXException, IOException { return (TransactionReport)parseEntity(TransactionReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  76 */   public static TransactionReport parse(String xml) throws SAXException, IOException { return (TransactionReport)parseEntity(TransactionReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  82 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  83 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  85 */       factory.setValidating(false);
/*  86 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  88 */       factory.newSAXParser().parse(is, handler);
/*  89 */       return (T)handler.getEntity();
/*  90 */     } catch (ParserConfigurationException e) {
/*  91 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  96 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/* 100 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/* 106 */     if (obj instanceof String) {
/* 107 */       String str = (String)obj;

/* 109 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/* 110 */         return (T)str.substring(1);
      }
/* 112 */       return obj;
    }
/* 114 */     if (obj instanceof InputStream) {
/* 115 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 117 */       in.mark(3);

/* 119 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 120 */         in.reset();
      }

/* 123 */       return (T)in;
/* 124 */     }  if (obj instanceof Reader) {
/* 125 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 127 */       in.mark(1);

/* 129 */       if (in.read() != 65279) {
/* 130 */         in.reset();
      }

/* 133 */       return (T)in;
    }
/* 135 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 141 */     if (value == null || value.length() == 0) {
/* 142 */       return defaultValue;
    }

/* 145 */     if (type == Boolean.class)
/* 146 */       return (T)Boolean.valueOf(value); 
/* 147 */     if (type == Integer.class)
/* 148 */       return (T)Integer.valueOf(value); 
/* 149 */     if (type == Long.class)
/* 150 */       return (T)Long.valueOf(value); 
/* 151 */     if (type == Short.class)
/* 152 */       return (T)Short.valueOf(value); 
/* 153 */     if (type == Float.class)
/* 154 */       return (T)Float.valueOf(value); 
/* 155 */     if (type == Double.class)
/* 156 */       return (T)Double.valueOf(value); 
/* 157 */     if (type == Byte.class)
/* 158 */       return (T)Byte.valueOf(value); 
/* 159 */     if (type == Character.class) {
/* 160 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 162 */     return (T)value;
  }




/* 168 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 173 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 178 */     if (uri == null || uri.length() == 0) {
/* 179 */       Object currentObj = this.m_objs.pop();
/* 180 */       String currentTag = (String)this.m_tags.pop();

/* 182 */       if (currentObj instanceof TransactionReport) {
/* 183 */         TransactionReport transactionReport = (TransactionReport)currentObj;

/* 185 */         if ("domain".equals(currentTag)) {
/* 186 */           transactionReport.addDomain(getText());
/* 187 */         } else if ("ip".equals(currentTag)) {
/* 188 */           transactionReport.addIp(getText());
        }
/* 190 */       } else if (currentObj instanceof TransactionType) {
/* 191 */         TransactionType type = (TransactionType)currentObj;

/* 193 */         if ("successMessageUrl".equals(currentTag)) {
/* 194 */           type.setSuccessMessageUrl(getText());
/* 195 */         } else if ("failMessageUrl".equals(currentTag)) {
/* 196 */           type.setFailMessageUrl(getText());
/* 197 */         } else if ("longestMessageUrl".equals(currentTag)) {
/* 198 */           type.setLongestMessageUrl(getText());
        }
/* 200 */       } else if (currentObj instanceof TransactionName) {
/* 201 */         TransactionName name = (TransactionName)currentObj;

/* 203 */         if ("successMessageUrl".equals(currentTag)) {
/* 204 */           name.setSuccessMessageUrl(getText());
/* 205 */         } else if ("failMessageUrl".equals(currentTag)) {
/* 206 */           name.setFailMessageUrl(getText());
/* 207 */         } else if ("longestMessageUrl".equals(currentTag)) {
/* 208 */           name.setLongestMessageUrl(getText());
        }
      }
    }

/* 213 */     this.m_text.setLength(0);
  }


/* 217 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 221 */   protected String getText() { return this.m_text.toString(); }


  private void parseForAllDuration(AllDuration parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 225 */     this.m_objs.push(parentObj);
/* 226 */     this.m_tags.push(qName);
  }

  private void parseForDuration(Duration parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 230 */     this.m_objs.push(parentObj);
/* 231 */     this.m_tags.push(qName);
  }

  private void parseForGraph(Graph parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 235 */     this.m_objs.push(parentObj);
/* 236 */     this.m_tags.push(qName);
  }

  private void parseForGraph2(Graph2 parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 240 */     this.m_objs.push(parentObj);
/* 241 */     this.m_tags.push(qName);
  }

  private void parseForGraphTrend(GraphTrend parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 245 */     this.m_objs.push(parentObj);
/* 246 */     this.m_tags.push(qName);
  }

  private void parseForMachine(Machine parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 250 */     if ("type".equals(qName)) {
/* 251 */       TransactionType type = this.m_maker.buildType(attributes);

/* 253 */       this.m_linker.onType(parentObj, type);
/* 254 */       this.m_objs.push(type);
    } else {
/* 256 */       throw new SAXException(String.format("Element(%s) is not expected under machine!", new Object[] { qName }));
    }

/* 259 */     this.m_tags.push(qName);
  }

  private void parseForName(TransactionName parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 263 */     if ("successMessageUrl".equals(qName) || "failMessageUrl".equals(qName) || "longestMessageUrl".equals(qName)) {
/* 264 */       this.m_objs.push(parentObj);
/* 265 */     } else if ("range".equals(qName)) {
/* 266 */       Range range = this.m_maker.buildRange(attributes);

/* 268 */       this.m_linker.onRange(parentObj, range);
/* 269 */       this.m_objs.push(range);
/* 270 */     } else if ("duration".equals(qName)) {
/* 271 */       Duration duration = this.m_maker.buildDuration(attributes);

/* 273 */       this.m_linker.onDuration(parentObj, duration);
/* 274 */       this.m_objs.push(duration);
/* 275 */     } else if ("graph".equals(qName)) {
/* 276 */       Graph graph = this.m_maker.buildGraph(attributes);

/* 278 */       this.m_linker.onGraph(parentObj, graph);
/* 279 */       this.m_objs.push(graph);
/* 280 */     } else if ("graph-trend".equals(qName)) {
/* 281 */       GraphTrend graphTrend = this.m_maker.buildGraphTrend(attributes);

/* 283 */       this.m_linker.onGraphTrend(parentObj, graphTrend);
/* 284 */       this.m_objs.push(graphTrend);
/* 285 */     } else if ("statusCode".equals(qName)) {
/* 286 */       StatusCode statusCode = this.m_maker.buildStatusCode(attributes);

/* 288 */       this.m_linker.onStatusCode(parentObj, statusCode);
/* 289 */       this.m_objs.push(statusCode);
    } else {
/* 291 */       throw new SAXException(String.format("Element(%s) is not expected under name!", new Object[] { qName }));
    }

/* 294 */     this.m_tags.push(qName);
  }

  private void parseForRange(Range parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 298 */     this.m_objs.push(parentObj);
/* 299 */     this.m_tags.push(qName);
  }

  private void parseForRange2(Range2 parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 303 */     this.m_objs.push(parentObj);
/* 304 */     this.m_tags.push(qName);
  }

  private void parseForStatusCode(StatusCode parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 308 */     this.m_objs.push(parentObj);
/* 309 */     this.m_tags.push(qName);
  }

  private void parseForTransactionReport(TransactionReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 313 */     if ("domain-names".equals(qName) || "domain".equals(qName) || "ips".equals(qName) || "ip".equals(qName)) {
/* 314 */       this.m_objs.push(parentObj);
/* 315 */     } else if ("machine".equals(qName)) {
/* 316 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 318 */       this.m_linker.onMachine(parentObj, machine);
/* 319 */       this.m_objs.push(machine);
    } else {
/* 321 */       throw new SAXException(String.format("Element(%s) is not expected under transaction-report!", new Object[] { qName }));
    }

/* 324 */     this.m_tags.push(qName);
  }

  private void parseForType(TransactionType parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 328 */     if ("successMessageUrl".equals(qName) || "failMessageUrl".equals(qName) || "longestMessageUrl".equals(qName)) {
/* 329 */       this.m_objs.push(parentObj);
/* 330 */     } else if ("name".equals(qName)) {
/* 331 */       TransactionName name = this.m_maker.buildName(attributes);

/* 333 */       this.m_linker.onName(parentObj, name);
/* 334 */       this.m_objs.push(name);
/* 335 */     } else if ("graph2".equals(qName)) {
/* 336 */       Graph2 graph2 = this.m_maker.buildGraph2(attributes);

/* 338 */       this.m_linker.onGraph2(parentObj, graph2);
/* 339 */       this.m_objs.push(graph2);
/* 340 */     } else if ("graph-trend".equals(qName)) {
/* 341 */       GraphTrend graphTrend = this.m_maker.buildGraphTrend(attributes);

/* 343 */       this.m_linker.onGraphTrend(parentObj, graphTrend);
/* 344 */       this.m_objs.push(graphTrend);
/* 345 */     } else if ("range2".equals(qName)) {
/* 346 */       Range2 range2 = this.m_maker.buildRange2(attributes);

/* 348 */       this.m_linker.onRange2(parentObj, range2);
/* 349 */       this.m_objs.push(range2);
    } else {
/* 351 */       throw new SAXException(String.format("Element(%s) is not expected under type!", new Object[] { qName }));
    }

/* 354 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 358 */     if ("transaction-report".equals(qName)) {
/* 359 */       TransactionReport transactionReport = this.m_maker.buildTransactionReport(attributes);

/* 361 */       this.m_entity = transactionReport;
/* 362 */       this.m_objs.push(transactionReport);
/* 363 */       this.m_tags.push(qName);
/* 364 */     } else if ("machine".equals(qName)) {
/* 365 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 367 */       this.m_entity = machine;
/* 368 */       this.m_objs.push(machine);
/* 369 */       this.m_tags.push(qName);
/* 370 */     } else if ("type".equals(qName)) {
/* 371 */       TransactionType type = this.m_maker.buildType(attributes);

/* 373 */       this.m_entity = type;
/* 374 */       this.m_objs.push(type);
/* 375 */       this.m_tags.push(qName);
/* 376 */     } else if ("name".equals(qName)) {
/* 377 */       TransactionName name = this.m_maker.buildName(attributes);

/* 379 */       this.m_entity = name;
/* 380 */       this.m_objs.push(name);
/* 381 */       this.m_tags.push(qName);
/* 382 */     } else if ("range".equals(qName)) {
/* 383 */       Range range = this.m_maker.buildRange(attributes);

/* 385 */       this.m_entity = range;
/* 386 */       this.m_objs.push(range);
/* 387 */       this.m_tags.push(qName);
/* 388 */     } else if ("duration".equals(qName)) {
/* 389 */       Duration duration = this.m_maker.buildDuration(attributes);

/* 391 */       this.m_entity = duration;
/* 392 */       this.m_objs.push(duration);
/* 393 */       this.m_tags.push(qName);
/* 394 */     } else if ("range2".equals(qName)) {
/* 395 */       Range2 range2 = this.m_maker.buildRange2(attributes);

/* 397 */       this.m_entity = range2;
/* 398 */       this.m_objs.push(range2);
/* 399 */       this.m_tags.push(qName);
/* 400 */     } else if ("graph".equals(qName)) {
/* 401 */       Graph graph = this.m_maker.buildGraph(attributes);

/* 403 */       this.m_entity = graph;
/* 404 */       this.m_objs.push(graph);
/* 405 */       this.m_tags.push(qName);
/* 406 */     } else if ("graph2".equals(qName)) {
/* 407 */       Graph2 graph2 = this.m_maker.buildGraph2(attributes);

/* 409 */       this.m_entity = graph2;
/* 410 */       this.m_objs.push(graph2);
/* 411 */       this.m_tags.push(qName);
/* 412 */     } else if ("graph-trend".equals(qName)) {
/* 413 */       GraphTrend graphTrend = this.m_maker.buildGraphTrend(attributes);

/* 415 */       this.m_entity = graphTrend;
/* 416 */       this.m_objs.push(graphTrend);
/* 417 */       this.m_tags.push(qName);
/* 418 */     } else if ("statusCode".equals(qName)) {
/* 419 */       StatusCode statusCode = this.m_maker.buildStatusCode(attributes);

/* 421 */       this.m_entity = statusCode;
/* 422 */       this.m_objs.push(statusCode);
/* 423 */       this.m_tags.push(qName);
    } else {
/* 425 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 431 */     if (uri == null || uri.length() == 0) {
/* 432 */       if (this.m_objs.isEmpty()) {
/* 433 */         parseRoot(qName, attributes);
      } else {
/* 435 */         Object parent = this.m_objs.peek();
/* 436 */         String tag = (String)this.m_tags.peek();

/* 438 */         if (parent instanceof TransactionReport) {
/* 439 */           parseForTransactionReport((TransactionReport)parent, tag, qName, attributes);
/* 440 */         } else if (parent instanceof Machine) {
/* 441 */           parseForMachine((Machine)parent, tag, qName, attributes);
/* 442 */         } else if (parent instanceof TransactionType) {
/* 443 */           parseForType((TransactionType)parent, tag, qName, attributes);
/* 444 */         } else if (parent instanceof TransactionName) {
/* 445 */           parseForName((TransactionName)parent, tag, qName, attributes);
/* 446 */         } else if (parent instanceof Range) {
/* 447 */           parseForRange((Range)parent, tag, qName, attributes);
/* 448 */         } else if (parent instanceof Duration) {
/* 449 */           parseForDuration((Duration)parent, tag, qName, attributes);
/* 450 */         } else if (parent instanceof Range2) {
/* 451 */           parseForRange2((Range2)parent, tag, qName, attributes);
/* 452 */         } else if (parent instanceof Graph) {
/* 453 */           parseForGraph((Graph)parent, tag, qName, attributes);
/* 454 */         } else if (parent instanceof Graph2) {
/* 455 */           parseForGraph2((Graph2)parent, tag, qName, attributes);
/* 456 */         } else if (parent instanceof GraphTrend) {
/* 457 */           parseForGraphTrend((GraphTrend)parent, tag, qName, attributes);
/* 458 */         } else if (parent instanceof AllDuration) {
/* 459 */           parseForAllDuration((AllDuration)parent, tag, qName, attributes);
/* 460 */         } else if (parent instanceof StatusCode) {
/* 461 */           parseForStatusCode((StatusCode)parent, tag, qName, attributes);
        } else {
/* 463 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 467 */       this.m_text.setLength(0);
    } else {
/* 469 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 475 */       return (new SimpleDateFormat(format)).parse(str);
/* 476 */     } catch (ParseException e) {
/* 477 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format) {
    try {
/* 483 */       return (new DecimalFormat(format)).parse(str);
/* 484 */     } catch (ParseException e) {
/* 485 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/transaction/model/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */